package net.mihome.app.ui;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.loopj.android.http.RequestParams;

import net.mihome.app.AppContext;
import net.mihome.app.R;
import net.mihome.app.bean.URLs;
import net.mihome.app.bean.User;
import net.mihome.app.bean.net.ResponsePojo;
import net.mihome.app.common.UIHelper;
import net.mihome.app.utils.MiAsyncHttpResponseHandler;
import net.mihome.app.utils.RequestParamsFactory;

import org.apache.http.Header;

import java.util.Map;

/**
 * Created by zhao on 2015/2/8.
 */
public class MiFuwuWuyeActivity extends BaseActivity {
    private User adminUser;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mifuwu_wuye_main);
        initAdminUserInfo();
    }
    public void baoxiu_to_add(View view){
        if(adminUser==null){
            Toast.makeText(this,"尚未获取到物业公司信息，请稍后..",Toast.LENGTH_SHORT).show();
            return ;
        }
        Intent intent=new Intent(this,MifuwuBaoxiuNewActivity.class);
        intent.putExtra("admin_user",adminUser);
        startActivity(intent);
    }
    public void baoxiu_to_history(View view){
        Intent intent=new Intent(this,MifuwuBaoxiuActivity.class);
        startActivity(intent);
    }
    /**
     * 初始化物业公司管理员信息
     * */
    public void initAdminUserInfo(){
        RequestParams requestParams= RequestParamsFactory.newInstance(AppContext.getInstance());
        AppContext.ASYNC_HTTP_CLIENT.get(this, URLs.MIHOME_USER_GET_COMMUNITY_ADMIN,requestParams,new MiAsyncHttpResponseHandler<Map>() {
            @Override
            public void onSuccess(int arg0, Header[] arg1, ResponsePojo<Map> responsePojo) {
                adminUser=User.parse(responsePojo.getData());
            }
            @Override
            public void onFailure(int arg0, Header[] arg1, Throwable arg3) {
                Toast.makeText(MiFuwuWuyeActivity.this, "请求物业管理员信息失败" + arg3.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }
    public void callWuye(View view){
        if(adminUser==null||adminUser.getMobile()==null||"".equals(adminUser.getMobile().trim())){
            Toast.makeText(this,"物业管理员信息无效,请于物业公司联系",Toast.LENGTH_SHORT).show();
            return;
        }

        String mobile=adminUser.getMobile().trim();
        if(adminUser.getCommunity()!=null&&adminUser.getCommunity().getBaoxiuMobile()!=null){
            mobile=adminUser.getCommunity().getBaoxiuMobile().trim();
        }
        String[] items=mobile.split(",|，|、");
        UIHelper.simpleDialog(this,items);
//        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + mobile));
//        startActivity(intent);
    }
}
